/*
 * $Id: MixedJDomStreamReader.java,v 1.3 2009/07/24 18:34:01 oliver Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package org.simplx.xstream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.jdom.Comment;
import org.jdom.Content;
import org.jdom.Element;
import org.jdom.Parent;

import com.thoughtworks.xstream.io.xml.JDomReader;

/**
 * A reader that manages mixed text and nodes in an XML stream.
 *
 * @author Ken Arnold
 */
@SuppressWarnings({"unchecked"})
public class MixedJDomStreamReader extends JDomReader
        implements MixedStreamReader {

    /**
     * Creates a new {@link MixedJDomStreamReader}.
     *
     * @param elem The element at the top of the subtree of XML that is managed
     *             by this reader.
     */
    public MixedJDomStreamReader(Element elem) {
        super(elem);
    }

    public Iterator partIterator() {
        Parent elem = (Parent) getCurrent();
        List kids = elem.getContent();
        List parts = new ArrayList<Object>();
        for (final Object child : kids) {
            if (child instanceof Element) {
                parts.add(new NodePart() {
                    public String nodeName() {
                        return ((Element) child).getName();
                    }
                });
            } else if(!(child instanceof Comment)){
                parts.add(new TextPart() {
                    public String contents() {
                        return ((Content) child).getValue();
                    }
                });
            }
        }
        return Collections.unmodifiableList(parts).iterator();
    }
}
